<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>nginx中location的规则 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>nginx中location的规则</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LNMP/">LNMP</a> <a class="article__tag-link" href="/tags/Nginx/">Nginx</a>
			</span>
		
	</div>

	

	
		<h2 id="语法规则"><a href="#语法规则" class="headerlink" title="语法规则"></a>语法规则</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">location [=|~|~*|^~] /uri/ &#123; … &#125;</div></pre></td></tr></table></figure>
<ul>
<li>= 开头表示精确匹配</li>
<li>^~ 开头表示uri以某个常规字符串开头，不是正则匹配。#^~表示普通字符匹配，如果该选项匹配，只匹配该选项，不匹配别的选项，一般用来匹配目录。nginx不对url做编码，因此请求为/static/20%/aa，可以被规则^~ /static/ /aa匹配到（注意是空格）。</li>
<li>~ 开头表示区分大小写的正则匹配</li>
<li>~*  开头表示不区分大小写的正则匹配</li>
<li>!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则</li>
<li>/ 通用匹配，任何请求都会匹配到。</li>
<li>@     #”@” 定义一个命名的 location，使用在内部定向时，例如 error_page, try_files如 error_page, try_files</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;多个location配置的情况下匹配顺序为：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先匹配 =，其次匹配^~, 其次是按文件中顺序的正则匹配，最后是交给 / 通用匹配。当有匹配成功时候，停止匹配，按当前匹配规则处理请求。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;”@”不能通过外部输入的url匹配<br>~ 这个指正则匹配</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例子，有如下匹配规则：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line">location = / &#123;精确匹配，必须是127.0.0.1/</div><div class="line"><span class="comment">#规则A</span></div><div class="line">&#125;</div><div class="line">location = /login &#123;精确匹配，必须是127.0.0.1/login</div><div class="line"><span class="comment">#规则B</span></div><div class="line">&#125;</div><div class="line">location ^~ /static/ &#123;非精确匹配，并且不区分大小写，比如127.0.0.1/static/js.</div><div class="line"><span class="comment">#规则C</span></div><div class="line">&#125;</div><div class="line">location ~ .(gif|jpg|png|js|css)$ &#123;区分大小写，以gif,jpg,js结尾</div><div class="line"><span class="comment">#规则D</span></div><div class="line">&#125;</div><div class="line">location ~* .png$ &#123;不区分大小写，匹配.png结尾的</div><div class="line"><span class="comment">#规则E</span></div><div class="line">&#125;</div><div class="line">location !~ .xhtml$ &#123;区分大小写，匹配不已.xhtml结尾的</div><div class="line"><span class="comment">#规则F</span></div><div class="line">&#125;</div><div class="line">location !~* .xhtml$ &#123;</div><div class="line"><span class="comment">#规则G</span></div><div class="line">&#125;</div><div class="line">location / &#123;什么都可以</div><div class="line"><span class="comment">#规则H</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="静态文件不记录日志，配置缓存"><a href="#静态文件不记录日志，配置缓存" class="headerlink" title="静态文件不记录日志，配置缓存"></a>静态文件不记录日志，配置缓存</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$</div><div class="line">&#123;</div><div class="line">        expires      30d;</div><div class="line">        access_log off;</div><div class="line">&#125;</div><div class="line"></div><div class="line">location ~ .*\.(js|css)$</div><div class="line"> &#123;</div><div class="line">        expires      12h;</div><div class="line">        access_log off;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/Nginx/29. nginx中location的规则/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
